<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleExport"
            color="rgb(146,226,206)"
            style="color: rgb(120, 120, 120); padding: 0 20px"
            v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item
          label=""
          prop=""
        >
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入所属用户"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="couponName"
        >
          <el-input
            v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="type"
        >
          <el-select
            v-model="queryParams.type"
            placeholder="请选择优惠券类型"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allType"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label=""
          prop="state"
        >
          <el-select
            v-model="queryParams.state"
            placeholder="请选择使用状态"
            clearable
            style="width: 150px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allState"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label=""
          prop="title"
        >
          <el-date-picker
            v-model="timearr"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="couponList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="领取时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="优惠券名称"
        align="center"
        prop="couponName"
      />
      <el-table-column
        label="优惠券类型"
        align="center"
        prop="type"
      >
        <template #default="scope">
          {{ scope.row.type==0 ? "通用打折券" : scope.row.type==1 ? "顺风车打折券" : scope.row.type==2 ? "成际打折券" : "" }}
        </template>
      </el-table-column>
      <el-table-column
        label="所属用户"
        align="center"
        prop="nickName"
      />
      <el-table-column
        label="优惠券面值"
        align="center"
        prop="couponAmount"
      />
      <el-table-column
          label="最低消费"
          align="center"
          prop="lowestConsume"
      />
      <el-table-column
        label="优惠券开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="优惠券结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否可用"
        align="center"
        prop="isUsable"
      >
        <template #default="scope">
          {{ scope.row.isUsable == 0 ? "不可用" : "可用" }}
        </template>
      </el-table-column>
      <el-table-column
        label="使用状态"
        align="center"
        prop="state"
      >
        <template #default="scope">
          {{ allState.find((item) => item.value == scope.row.state).label }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Receive">
import {
  listReceive,
  delReceive,
  addReceive,
  updateReceive,
} from "@/api/system/receive";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const timearr = ref(null);
const couponList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const createTime = ref([]);

const data = reactive({
  allState: [
    {
      label: "待使用",
      value: 0,
    },
    {
      label: "已使用",
      value: 1,
    },
    {
      label: "已过期",
      value: 2,
    },
  ],
  allType: [
    {
      label: "通用打折券",
      value: 0,
    },
    {
      label: "顺风车打折券",
      value: 1,
    },
    {
      label: "成际打折券",
      value: 2,
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    couponName: null,
    type: null,
    state: null,
  },
  rules: {
    state: [{ required: true, message: "状态不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules, allState, allType } = toRefs(data);

/** 查询优惠券列表 */
function getList() {
  loading.value = true;
  listReceive(queryParams.value).then((response) => {
    console.log(response,'返回的优惠券领取记录')
    couponList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    timearr.value = null

  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    receiveId: null,
    couponName: null,
    type: null,
    couponAmount: null,
    lowestConsume: null,
    availableDate: null,
    state: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("couponRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  console.log(timearr.value,'时间数组dddddd')
  if(timearr.value){
    queryParams.value.startSift = timearr.value[0];
    queryParams.value.endSift = timearr.value[1];
  }else{
    queryParams.value.startSift = "";
    queryParams.value.endSift = "";
  }
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.receiveId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加优惠券";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改优惠券";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["couponRef"].validate((valid) => {
    if (valid) {
      if (form.value.receiveId != null) {
        updateReceive(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReceive(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _receiveIds = row.receiveId || ids.value;
  proxy.$modal
    .confirm('是否确认删除优惠券编号为"' + _receiveIds + '"的数据项？')
    .then(function () {
      return delReceive(_receiveIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/coupon/export",
    {
      ...queryParams.value,
    },
    `coupon_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
